// pages/myinfo/customserver/computerFault/computerFault.js
Component({
     properties:{
         msg:{
             type:Array,
             value:[],
             
             },
          img:{
              type:Object,
              value:{
                  img: ''
              }
          }   
             
     },  
    /**
     * 页面的初始数据
     */

    data: {
        checkKey:[],
        uploadImg: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
   methods:{
    clearLoadImg(e){
        let index = e.currentTarget.dataset.clear    
        //console.log(index)   
        this.data.uploadImg.splice(index,1)   
        //console.log(this.data.uploadImg)  
         this.setData({
             uploadImg: this.data.uploadImg
         })
        this.triggerEvent('clearLoadImg', this.data.uploadImg)
    },
    userText(e){
        this.triggerEvent('userText',e.detail.value)
    },
    
    // submitBtn(){
    //     console.log('组件')
    // },
    changeColor(e) {
        let _this = this;
        let index = e.currentTarget.dataset.index;
        if (this.data.msg[index].state == 1) {
            _this.data.msg[index].state = 0;
            _this.data.checkKey.splice(this.data.checkKey.indexOf(index), 1)
        } else if (this.data.msg[index].state == 0) {
            this.data.msg[index].state = 1;
            _this.data.checkKey.push(index)
        }
        _this.triggerEvent('changeColor', _this.data.checkKey)
        this.setData({
            msg: this.data.msg
        })
        
    },
    
    clickload() {
        var that = this;
        let uploadImg = this.data.uploadImg;
        wx.chooseImage({
            count: 4 - uploadImg.length,  //最多可以选择的图片总数  
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
                var tempFilePaths = res.tempFilePaths;
                if (uploadImg.length !== 4) {
                    uploadImg = uploadImg.concat(tempFilePaths)
                    that.setData({
                        uploadImg: uploadImg
                    })
                    that.triggerEvent('clickload',uploadImg)
                } 
                //启动上传等待中...  
                // wx.showToast({
                //     title: '正在上传...',
                //     icon: 'loading',
                //     mask: true,
                //     duration: 10000
                // })
                // var uploadImgCount = 0;
                // for (var i = 0, h = tempFilePaths.length; i < h; i++) {
                //     wx.uploadFile({
                //         url: util.getClientSetting().domainName + '/home/uploadfilenew',
                //         filePath: tempFilePaths[i],
                //         name: 'uploadfile_ant',
                //         formData: {
                //             'imgIndex': i
                //         },
                //         header: {
                //             "Content-Type": "multipart/form-data"
                //         },
                //         success: function (res) {
                //             uploadImgCount++;
                //             var data = JSON.parse(res.data);
                //             //服务器返回格式: { "Catalog": "testFolder", "FileName": "1.jpg", "Url": "https://test.com/1.jpg" }  
                //             var productInfo = that.data.productInfo;
                //             if (productInfo.bannerInfo == null) {
                //                 productInfo.bannerInfo = [];
                //             }
                //             productInfo.bannerInfo.push({
                //                 "catalog": data.Catalog,
                //                 "fileName": data.FileName,
                //                 "url": data.Url
                //             });
                //             that.setData({
                //                 productInfo: productInfo
                //             });

                //             //如果是最后一张,则隐藏等待中  
                //             if (uploadImgCount == tempFilePaths.length) {
                //                 wx.hideToast();
                //             }
                //         },
                //         fail: function (res) {
                //             wx.hideToast();
                //             wx.showModal({
                //                 title: '错误提示',
                //                 content: '上传图片失败',
                //                 showCancel: false,
                //                 success: function (res) { }
                //             })
                //         }
                //     });
                // }
            }
        });
    }
   } 
   
})